//
//  XMConversationBgController.m
//  XunMi
//
//  Created by MacBook on 2024/4/29.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMConversationBgController.h"
#import "XMConversationBgModel.h"
#import "XMConBgCollectionViewCell.h"

@interface XMConversationBgController ()
@property (nonatomic, strong) NSMutableArray <XMConversationBgModel *> *dataArray;
@property (nonatomic, strong) XMConversationBgModel *selModel;
@end

@implementation XMConversationBgController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initVariables];
    [self setupSubviews];
    [self listData];
}

- (void)listData {
    [MBProgressHUD showHUDActivityMsg:nil  toView:kWindow];
    NSMutableDictionary *dic = @{
        @"target_user_id": self.target_user_id
    }.mutableCopy;
    
    [YHNetworkManager.sharedInstance postWithUrl:@"api/chat/chatCover/list" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
        if (tip.success) {
            [MBProgressHUD hideHUD];
            self.dataArray = [NSMutableArray arrayWithArray:[XMConversationBgModel mj_objectArrayWithKeyValuesArray:result[@"record"]]];
            [self.collectionView reloadData];
        }
    }];
}

#pragma mark - 初始化变量
- (void)initVariables {
    [self registerCollectionNibWithNibName:kStringFromClass(XMConBgCollectionViewCell)];
}
#pragma mark - setupSubViews
- (void)setupSubviews {
    self.navTitleLa.text = @"选择背景图";
    
    [self.navLeftBtn setImage:nil forState:UIControlStateNormal];
    [self.navLeftBtn setTitle:@"取消" forState:UIControlStateNormal];
    [self.navLeftBtn setTitleColor:kHEX(@"#686868") forState:UIControlStateNormal];
    self.navLeftBtn.titleLabel.font = kUIFont(15);
    [self.navLeftBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(50, 44));
    }];
    
    [self.navRightBtn setTitle:@"完成" forState:UIControlStateNormal];
    [self.navRightBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(66, 32));
    }];
    [self.navRightBtn setBackgroundColor:kAppMainColor];
    [self.navRightBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [self.navRightBtn addTarget:self action:@selector(finish) forControlEvents:UIControlEventTouchUpInside];
    
    kCornerRadius(self.navRightBtn, 16, 0, UIColor.clearColor);
}
#pragma mark - viewBlock
- (void)viewBlock {
    
}

#pragma mark - UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XMConBgCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kStringFromClass(XMConBgCollectionViewCell) forIndexPath:indexPath];
    cell.model = [self.dataArray safe_objectAtIndex:indexPath.item];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [self.dataArray enumerateObjectsUsingBlock:^(XMConversationBgModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (indexPath.item == idx) {
            obj.selected = YES;
            self.selModel = obj;
        } else {
            obj.selected = NO;
        }
    }];
    [self.collectionView reloadData];
}

#pragma mark - XMCollectionFlowLayoutD
// 一行显示N个
- (NSInteger)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout numberOfColumnInSection:(NSInteger)section {
    return 3;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout itemWidth:(CGFloat)width heightForItemAtIndexPath:(NSIndexPath *)indexPath {
    return (kScreenWidth - 60) / 3;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(4, 20, 20, 20);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout referenceHeightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout referenceHeightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

#pragma mark - Actions
- (void)finish {
    if (self.selModel) {
        [MBProgressHUD showHUDActivityMsg:nil  toView:kWindow];
        NSMutableDictionary *dic = @{
            @"chat_cover_id": self.selModel.index,
            @"target_user_id": self.target_user_id
        }.mutableCopy;
        
        [YHNetworkManager.sharedInstance postWithUrl:@"api/chat/chatCover/edit" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
            if (tip.success) {
                [MBProgressHUD hideHUD];
                if (self.finishBlock) {
                    self.finishBlock();
                }
                [self backAction];
            }
        }];
    } else {
        [self backAction];
    }
}

@end
